package com.jasonm.rssminer.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jasonm.rssminer.dto.UserDTO;
import com.jasonm.rssminer.service.RssService;
import com.jasonm.rssminer.utils.GenerateOutput;

@Controller
public class MainController {

	private static final Logger log = Logger.getLogger(MainController.class);
	
	@Autowired
	@Qualifier("rssService")
	private RssService rssService;

	@Autowired
	@Qualifier("generateOutput")
	private GenerateOutput generateOutput;

	@RequestMapping(value = "/userid/{userid}", method = RequestMethod.GET)
	@ResponseBody
	public String getAllMySearches(@PathVariable String userid) {

		return generateOutput.mapToJSON(rssService.getUserSearches(Integer
				.parseInt(userid)));

	}
	
	@RequestMapping(value="/userid/all",method = RequestMethod.GET)
	@ResponseBody
	public String getAllUsers(){
		return generateOutput.mapToJSON(rssService.getAllUsers());
	}

	@RequestMapping(value = "/email/{email}/password/{password}", method = RequestMethod.GET)
	@ResponseBody
	public String loginUser(@PathVariable String email,
			@PathVariable String password) {
		UserDTO userDTO = rssService.getUserId(email);
		if (userDTO.getPassword().equals(password)) {
			return generateOutput.mapToJSON(userDTO);
		}
		return null;
	}

	/**
	 * 
	 * Handle Any Null Pointer Exceptions
	 * 
	 * 
	 * 
	 * @param exception
	 *            Null Pointer
	 * @param response
	 *            Internal Server Error
	 */

	@ExceptionHandler(NullPointerException.class)
	public void handleNullPointerException(Throwable exception,
			HttpServletResponse response) {
		if (log.isDebugEnabled()) {
			log.debug("Data Exception Happening:" + exception.getMessage());
		}
		sendResponseError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
				response);
	}

	/**
	 * 
	 * Handle Any Data Access Exceptions
	 * 
	 * 
	 * 
	 * @param exception
	 *            Data Access
	 * @param response
	 *            Not Found
	 */
	@ExceptionHandler(DataAccessException.class)
	public void handleDAOException(Throwable exception,
			HttpServletResponse response) {

		if (log.isDebugEnabled()) {
			log.debug("Data Exception Happening:" + exception.getMessage());
		}
		sendResponseError(HttpServletResponse.SC_NOT_FOUND, response);

	}

	/**
	 * 
	 * Handle Any IO Exceptions
	 * 
	 * 
	 * 
	 * @param exception
	 *            IO
	 * @param response
	 *            Bad IO
	 */
	@ExceptionHandler(IOException.class)
	public void handleValidationException(Throwable exception,
			HttpServletResponse response) {
		if (log.isDebugEnabled()) {
			log.debug("IO Exception Happening:" + exception.getMessage());
		}
		sendResponseError(HttpServletResponse.SC_BAD_REQUEST, response);
	}

	/**
	 * Send the error message
	 * 
	 * @param errorNum
	 *            HTTP Error Number
	 * @param response
	 *            Http Error Number to requester
	 */
	private void sendResponseError(int errorNum, HttpServletResponse response) {
		try {
			response.sendError(errorNum);
		} catch (IOException e) {
			log.error("Problem writting out error message:" + e.getMessage());
		}

	}
	
}